﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.Xna.Framework;
using Rectangle = Microsoft.Xna.Framework.Rectangle;
using Color = Microsoft.Xna.Framework.Color;
using ButtonState = Microsoft.Xna.Framework.Input.ButtonState;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using WeifenLuo.WinFormsUI.Docking;

namespace Brain.Editor
{
    public partial class VisualScriptMaker : DockContent
    {
        public ContextMenuStrip contextStrip;
        public MainEditor MainEditor;

        public SpriteBatch SpriteBatch;
        public Texture2D PixelTexture;

        public SpriteFont SpriteFont;        

        public VisualScriptMaker(MainEditor form)
        {
            SpriteFont = Engine.Instance.ContentManager.Load<SpriteFont>("Arial14");

            SpriteBatch = new SpriteBatch(Engine.Instance.GraphicsDevice);
            PixelTexture = new Texture2D(Engine.Instance.GraphicsDevice, 1, 1);
            PixelTexture.SetData(new Color[] { Color.White });

            this.MainEditor = form;

            InitializeComponent();

            this.visualScriptControl1.OnDraw += new SetVoid(visualScriptControl1_OnDraw);

            contextStrip = new ContextMenuStrip();
            this.ContextMenuStrip = contextStrip;

            ToolStripMenuItem conditions = AddItem("Conditions");
            AddItem(conditions, "Always", conditions_Always);
            AddItem(conditions, "Timed", conditions_Timed);

            ToolStripMenuItem objects = AddItem("Objects");

            ToolStripMenuItem actors = AddItem("Actors");

        }        
        private void conditions_Always(object sender, EventArgs e)
        {
            new VisualScriptCondition_Always(this.visualScriptControl1.VisualScript);
        }
        private void conditions_Timed(object sender, EventArgs e)
        {
            new VisualScriptCondition_Timed(this.visualScriptControl1.VisualScript);
        }

        public Color BorderColor = new Color(80, 80, 80);
        public Color BackgroundColor = new Color(128, 128, 128);
        public Color BlockColor = Color.Green;
        public int BorderSize = 2;


        private VisualScriptNode Node;
        private bool Modifying = false;
        private Vector2 Difference;

        private MouseState LastState;
        private MouseState State;
        void visualScriptControl1_OnDraw()
        {
            Mouse.WindowHandle = this.visualScriptControl1.Handle;

            LastState = State;
            State = Mouse.GetState();
            bool clicking = State.LeftButton == ButtonState.Pressed;
            if (!clicking)
            {
                Modifying = false;
                Node = null;
            }
            Vector2 MousePos = Vector2.Transform(new Vector2(State.X, State.Y), Matrix.Invert(this.visualScriptControl1.Camera.World));
            Vector2 Dif = new Vector2(LastState.X - State.X, LastState.Y - State.Y);
            if (State.MiddleButton == ButtonState.Pressed)
            {
                this.visualScriptControl1.Camera.Position -= Dif;
            }

            SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, null, null, null, null, this.visualScriptControl1.Camera.World);
            for (int i = 0; i < this.visualScriptControl1.VisualScript.AllNodes.Count; i++)
            {
                VisualScriptNode node = this.visualScriptControl1.VisualScript.AllNodes[i];
                if (node is VisualScriptNode_Condition)
                {
                    // condition
                    int height = 70;
                    int width = 100;
                    int stringX = 5;
                    int stringY = 20;
                    int blockSize = 10;

                    Vector2 size = SpriteFont.MeasureString(node.Name);
                    int additional = (int)size.X + stringX + BorderSize;

                    int numb = node.NeededObjectSlots - 3;
                    if (numb < 0)
                    {
                        numb = 0;
                    }
                    int add2 = width + (numb * 30);
                    int toAdd = Math.Max(add2, additional);

                    width = Math.Max(toAdd, width);

                    // Draw Background
                    int border = BorderSize * 2;
                    Rectangle BorderRectangle = new Rectangle((int)node.Position.X, (int)node.Position.Y, width, height);

                    if (clicking && BorderRectangle.Contains((int)MousePos.X, (int)MousePos.Y)
                        && Node == null ||
                        Modifying && Node == node)
                    {
                        Modifying = true;
                        if (Node == null)
                        {
                            Difference = node.Position - MousePos;
                        }
                        node.Position = MousePos + Difference;
                        Node = node;

                        BorderRectangle.X = (int)node.Position.X;
                        BorderRectangle.Y = (int)node.Position.Y;
                    }

                    SpriteBatch.Draw(PixelTexture, BorderRectangle, BorderColor);
                    SpriteBatch.Draw(PixelTexture, new Rectangle((int)node.Position.X + BorderSize, (int)node.Position.Y + BorderSize, width - border, height - border), BackgroundColor);
                    SpriteBatch.DrawString(SpriteFont, node.Name, new Vector2(node.Position.X + stringX, node.Position.Y + stringY), Color.White);
                    SpriteBatch.Draw(PixelTexture, new Rectangle((int)node.Position.X + width - blockSize, (int)node.Position.Y + blockSize, blockSize, blockSize), BlockColor);
                }
                else if (node is VisualScriptNode_Actor)
                {
                    // actor
                }
                else if (node is VisualScriptNode_Value)
                {
                    // value
                }
            }
            SpriteBatch.End();
        }

        public void AddItem(ToolStripMenuItem parent, string text, EventHandler handler)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text);
            item.Click += handler;
            parent.DropDownItems.Add(item);
        }

        public void AddItem(string text, EventHandler handler)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text);
            item.Click += handler;
            this.ContextMenuStrip.Items.Add(item);
        }
        public ToolStripMenuItem AddItem(string text)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(text);
            this.ContextMenuStrip.Items.Add(item);

            return item;
        }


    }
}
